package cn.wolfcode.web.controller;

import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Announcement;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IAnnouncementService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IAnnouncementService announcementService;

    @RequestMapping("/LLogin")
    @ResponseBody
    public JsonResult LLogin(String username, String password, HttpSession session){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        //System.out.println(session.getAttribute("num"));
//        if(session.getAttribute("num")){
//            jsonResult.setMsg("请稍后再试");
//            return jsonResult;
//        }
        Employee employee = employeeService.checkLogin(username, password);
        jsonResult.setMsg("登录成功");
        jsonResult.setSuccess(true);
        session.setAttribute("EMP_IN_SESSION",employee);
        List<String> expressions = employeeService.getAllPermission(employee.getId());
        session.setAttribute("EXPRESSION_IN_SESSION",expressions);

        List<Announcement> announcements = announcementService.selectNoRead();
        session.setAttribute("NOREAD_IN_SESSION",announcements);

        //公告未读时间
//        ArrayList<String> list = new ArrayList<>();
//        for (Announcement announcement : announcements) {
//            Date date = announcement.getCreateTime();
//            String s = DateUtil.formatBetween(new Date(), date, BetweenFormater.Level.MINUTE);
//        }

        return jsonResult;
    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        //删当前员工所有session
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    public String nopermission(){
        return "/common/nopermission";
    }

    @RequestMapping("/changePassword")
    public String changePassword(){
        return "/employee/changePassword";
    }

    @RequestMapping("/savePassword")
    @ResponseBody
    public JsonResult savePassword(HttpSession session, String password_old, String password_new){
        JsonResult jsonResult = new JsonResult();
        Employee emp_in_session = (Employee)session.getAttribute("EMP_IN_SESSION");
        Long id = emp_in_session.getId();
        String password = emp_in_session.getPassword();
        if(!password.equals(password_old)){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("原密码错误");
            return jsonResult;
        }
        if(StringUtils.hasText(password_new)){
            employeeService.updatePassword(id,password_new);
            jsonResult.setSuccess(true);
            jsonResult.setMsg("修改成功");
            //退出当前用户
            session.invalidate();
        }
        return jsonResult;
    }



}
